//
//  SearchController.m
//  网易新闻
//
//  Created by dllo on 15/9/30.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "SearchController.h"
#import "AFTool.h"
#import "DetailsController.h"
#import "MJRefresh.h"
#import "Settings.h"
@interface SearchController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UISearchBar *search;
@property(nonatomic, retain)NSArray *dicArr;
@property(nonatomic, retain)UITableView *table;
@end

@implementation SearchController

- (void)viewWillAppear:(BOOL)animated{
    self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.search.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.table.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.tabBarController.tabBar.hidden = YES;
    [self.table reloadData];
}

- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 80, 375, 40)];
    UIView *view = [self.search.subviews objectAtIndex:0];
    [[view.subviews objectAtIndex:0]removeFromSuperview];
    [self.search setPlaceholder:@"请输入关键字..."];
    self.search.searchBarStyle = UIBarMetricsDefault;
    self.search.delegate = self;
    [self.view addSubview:self.search];
    [self.search becomeFirstResponder];
    [self.search release];
    
    [self createTableView];
    self.dicArr = [[NSArray alloc]init];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.search resignFirstResponder];
    [AFTool GETWithURL:[[NSString stringWithFormat:@"http://c.3g.163.com/search/comp/MA==/40/%@.html?deviceId=ODYyMDk1MDIxNjc1NTcx&version=bmV3c2NsaWVudC41LjMuMy5hbmRyb2lk&channel=VDEzNDg2NDc5MDkxMDc=", [[self.search.text dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        self.dicArr = [[tempDic objectForKey:@"doc"]objectForKey:@"result"];
        [tempDic release];
        [self.table reloadData];
    } fail:^(NSError *error) {
        NSLog(@"404(SearchController)");
    }];
}

- (void)createTableView{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 160, 375, 500) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.tableFooterView = [[UIView alloc]init];
    self.table.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

        [self.table.footer endRefreshing];
    }];
    [self.view addSubview:self.table];
    [self.table release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"]autorelease];
    NSDictionary *dic = [self.dicArr objectAtIndex:indexPath.row];
    NSString *title = [[[dic objectForKey:@"title"]stringByReplacingOccurrencesOfString:@"/" withString:@""]stringByReplacingOccurrencesOfString:@"<em>" withString:@""];
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:title];
    [attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:[title rangeOfString:self.search.text options:NSCaseInsensitiveSearch]];
    cell.backgroundColor = [[Settings shareSettings]color:WhiteToLightGray];
    cell.textLabel.attributedText = attributeString;
    cell.textLabel.textColor = [[Settings shareSettings]color:BlackToWhite];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.text = [dic objectForKey:@"ptime"];
    cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dicArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailsController *result = [[DetailsController alloc]init];
    result.docid = [self.dicArr[indexPath.row]objectForKey:@"docid"];;
    [self.navigationController pushViewController:result animated:YES];
    [result release];
}

- (void)dealloc{
    _search.delegate = nil;
    _table.delegate = nil;
    _table.dataSource = nil;
    [_search release];
    [_dicArr release];
    [_table release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
